﻿<UserControl x:Class="GSF.PhasorProtocols.UI.UserControls.RealTimeStatisticUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" d:DesignHeight="590" d:DesignWidth="850"
             xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:uc="clr-namespace:GSF.TimeSeries.UI.UserControls;assembly=GSF.TimeSeries.UI.WPF"
             xmlns:tsfBinding="clr-namespace:GSF.TimeSeries.UI.Binding;assembly=GSF.TimeSeries.UI"
             xmlns:tsf="clr-namespace:GSF.TimeSeries.UI;assembly=GSF.TimeSeries.UI.WPF"
             xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
             Background="Transparent">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/GSF.TimeSeries.UI.WPF;Component/Resources/CommonResource.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <StackPanel Orientation="Vertical">

        <StackPanel.Resources>

            <HierarchicalDataTemplate x:Key="MeasurementTemplate">
                
                <Border BorderThickness="0,0,0,1" BorderBrush="#FFDDDDDD">
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                        <StackPanel.Resources>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="HorizontalAlignment" Value="Left"/>
                                <Setter Property="TextAlignment" Value="Left"/>
                                <Setter Property="VerticalAlignment" Value="Center"/>
                            </Style>
                        </StackPanel.Resources>

                        <TextBlock Text="{tsfBinding:Column Path=ID}" Width="100"/>
                        <TextBlock Text="{tsfBinding:Column Path=SignalReference}" Width="200"/>
                        <TextBlock Text="{tsfBinding:Column Path=StatisticName}" ToolTip="{tsfBinding:Column Path=StatisticDescription}" Width="200" TextWrapping="Wrap"/>
                        <TextBlock Text="{tsfBinding:Column Path=TimeTag}" FontWeight="Bold" Width="80" HorizontalAlignment="Center" TextAlignment="Center"/>
                        <TextBlock Text="{tsfBinding:Column Path=Value}" FontWeight="Bold" Width="210"
                                   Foreground="{tsfBinding:Column Path=Quality, Converter={StaticResource StringToStatusColorConverter}}" HorizontalAlignment="Center" TextAlignment="Center"/>
                    </StackPanel>
                </Border>
                
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate x:Key="DeviceTemplate" ItemsSource="{tsfBinding:Column Path=StatisticMeasurementList}" ItemTemplate="{StaticResource MeasurementTemplate}">
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">

                    <StackPanel.Resources>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Left"/>
                            <Setter Property="TextAlignment" Value="Left"/>
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </StackPanel.Resources>

                    <TextBlock Text="{tsfBinding:Column Path=DeviceAcronym}" Width="145" FontWeight="Bold"/>
                    <TextBlock Text="{tsfBinding:Column Path=DeviceName}" Width="550" FontWeight="Bold"/>                    
                </StackPanel>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate x:Key="StreamTemplate" ItemsSource="{tsfBinding:Column Path=DeviceStatisticList}" ItemTemplate="{StaticResource DeviceTemplate}">

                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                    
                    <StackPanel.Resources>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Left"/>
                            <Setter Property="TextAlignment" Value="Left"/>
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </StackPanel.Resources>

                    <Ellipse Width="12" Height="12" VerticalAlignment="Center" Fill="{tsfBinding:Column Path=StatusColor, Converter={StaticResource StringToStatusColorConverter}}" Margin="0,0,5,0"/>
                    <TextBlock Text="{tsfBinding:Column Path=Acronym}" Width="145" FontWeight="Bold"/>
                    <TextBlock Text="{tsfBinding:Column Path=Name}" Width="575" FontWeight="Bold"/>
                    
                </StackPanel>
                
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate x:Key="RootNodeTemplate" ItemsSource="{tsfBinding:Column Path=StreamStatisticList}" ItemTemplate="{StaticResource StreamTemplate}">

                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">

                    <StackPanel.Resources>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Left"/>
                            <Setter Property="TextAlignment" Value="Left"/>
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </StackPanel.Resources>

                    <TextBlock Text="{tsfBinding:Column Path=SourceType}" Width="755" FontWeight="Bold"/>
                    
                </StackPanel>

            </HierarchicalDataTemplate>

            <!--<Style x:Key="ExpandedItemStyle" TargetType="TreeViewItem">
                <Setter Property="IsExpanded" Value="{tsfBinding:Column Path=DataContext.Expanded, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl, AncestorLevel=1}}"/>
            </Style>-->
            <Style x:Key="ExpandedItemStyle" TargetType="TreeViewItem">
                <Setter Property="IsExpanded" Value="{tsfBinding:Column Path=Expanded}"/>
            </Style>

        </StackPanel.Resources>

        <StackPanel Orientation="Vertical" >
            <tsf:TsfPopup x:Name="PopupSettings" Topmost="False" Placement="Center">

                <Border CornerRadius="5" BorderThickness="1" Background="White" BorderBrush="Blue" >

                    <Grid>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="2*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <TextBlock Text="Statistic Data Refresh Interval (in seconds)" Grid.Column="0" Grid.Row="0"/>
                        <TextBox x:Name="TextBoxRefreshInterval" Grid.Row="0" Grid.Column="1" Width="50"/>

                        <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Right">
                            <Button x:Name="ButtonRestore" Content="Restore Default" Margin="2" Width="Auto" Click="ButtonRestore_Click"/>
                            <Button Content="Cancel" Margin="2" Click="ButtonCancel_Click"/>
                            <Button x:Name="ButtonSave" Content="Save" Margin="2" IsDefault="True" Click="ButtonSave_Click"/>
                        </StackPanel>

                    </Grid>

                </Border>

            </tsf:TsfPopup>

            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Button x:Name="ButtonDisplaySettings" Content="Display Settings" Template="{StaticResource HyperlinkStyleButtonTemplate}" 
                     Width="Auto" Click="ButtonDisplaySettings_Click"/>
            <TextBlock Text="Refresh Interval: " Width="Auto" />                
            <TextBlock x:Name="TextBlockMeasurementRefreshInterval" Width="Auto"/>                
            <TextBlock Text="{Binding Path=DataContext.LastRefresh, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl, AncestorLevel=1}}" />
        </StackPanel>

            <TreeView ItemsSource="{tsfBinding:Column Path=ItemsSource}" ItemTemplate="{StaticResource RootNodeTemplate}" ItemContainerStyle="{StaticResource ExpandedItemStyle}"
                      Height="610" VerticalAlignment="Top" HorizontalAlignment="Left" Width="850" ScrollViewer.HorizontalScrollBarVisibility="Disabled"/>

      </StackPanel>

    </StackPanel>

</UserControl>
